﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.DAO;
using System.Data.SqlClient;
using fideeraSoft.Utils;
using fideeraSoft.Entities;

namespace fideeraSoft.DAOimpl
{
    public class LoteDAOSQLimpl:LoteDAO
    {

        #region BUSCAR ULTIMO ID DE LOTE
        public int buscarNuevoUltimoIdLoteEnBD()
        {
            int ID = 0;
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "SELECT TOP 1 ID FROM Lote ORDER BY ID DESC ";
                SqlCommand cmd = new SqlCommand(query, conn);

                SQLclass.commandQuery(conn, cmd, query);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {

                    while (reader.Read())
                    {
                        ID = reader.GetInt32(0);
                        //  CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] ID : " + ID, CLogger.ERROR);      //DEBUG
                    }
                    ID++;
                }
                else
                {
                    ID = 1;
                }
            }
            catch (Exception e)
            {
                CLogger.Append("[buscarNuevoUltimoIdLoteEnBD] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[buscarNuevoUltimoIdLoteEnBD] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
            // CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] RETURN ID : " + ID, CLogger.ERROR);      //DEBUG
            return ID;
        }

        #endregion

      

        #region ALTA Lote
        public void altaLote(Lote lote)
        {
            SqlConnection conn = null;
            try
            {
                conn = SQLclass.conectarSQL();
                conn.Open();

                string query = "SET IDENTITY_INSERT Lote ON; INSERT INTO Lote (ID,codigo,estado,fechaVencimiento,fechaRecepcion) VALUES (@IdLote,@Codigo,@Estado,@FechaVencimiento,@FechaRecepcion)";
                
                SqlCommand cmd = new SqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@IdLote", lote.IdLote);
                cmd.Parameters.AddWithValue("@Codigo", lote.Codigo);

                cmd.Parameters.AddWithValue("@Estado", 1);

                cmd.Parameters.AddWithValue("@FechaVencimiento",lote.FechaVencimiento );
                cmd.Parameters.AddWithValue("@FechaRecepcion", lote.FechaRecepcion);

                cmd.ExecuteNonQuery();
                //cmd.Parameters.Clear();
                // cmd.CommandText = "SELECT @@IDENTITY";
                // int insertID=Convert.ToInt32(cmd.ExecuteScalar());

                //  cmd.Dispose();
                cmd = null;             

            }
            catch (Exception e)
            {
                CLogger.Append("[altaLote] Exception alta: " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[altaLote] Exception alta: " + e);
            }
            finally
            {
                conn.Close();
            }

        }

        #endregion

        #region Eliminar LOTE POR ID

        public void eliminarLotePorId(int id)
        {
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "DELETE FROM Lote WHERE ID='" + id.ToString()+"'";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                CLogger.Append("[eliminarLotePorId] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[eliminarLotePorId] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

        #region Eliminar TODOS LOS LOTES EN DB

        public void eliminarTodosLosLotesEnDb()
        {
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "DELETE FROM Lote";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                CLogger.Append("[eliminarTodosLosLotesEnDb] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[eliminarTodosLosLotesEnDb] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion
    }
}
